<?php


namespace app\appa\controller;


use app\appa\model\AddressModel;
use app\appa\model\MasterModel;
use app\appa\model\MastserModel;
use app\appa\model\MembersModel;
use app\appa\model\OrderModel;
use app\appa\model\PetModel;
use app\appa\model\PriceModel;
use app\appa\validate\OrderValidate;
use app\common\controller\Api;
use think\Request;

class Order extends Api
{
    //查看全部订单
    public function index(Request $request)
    {
        $order = OrderModel::order('id', 'DESC')
            ->where('members_id',$request->userId)
            ->field('id,pet_image,price_name,status,pet_type,price')
            ->select();
//        halt($request->userId);
        if ($order) {
            $this->success('查询成功', [
                'order' => $order
            ]);
        } else {
            $this->error('查询失败');
        }
//        halt($request->userId);
    }
    //查看全部订单的待支付
    public function index1(Request $request)
    {
        $order = OrderModel::order('id', 'DESC')
            ->where('members_id',$request->userId)
                ->where('pay_type',0)
            ->field('id,pet_image,price_name,status,pet_type,price')
            ->select();
        if ($order) {
            $this->success('查询成功', [
                'order' => $order
            ]);
        } else {
            $this->error('查询失败');
        }
    }
    //查看全部订单的待接单
    public function index2(Request $request)
    {
        $order = OrderModel::order('id', 'DESC')
            ->where('members_id',$request->userId)
            ->where('switch',0)
            ->where('pay_type','<>',0)
            ->field('id,pet_image,price_name,status,pet_type,price')
            ->select();
        if ($order) {
            $this->success('查询成功', [
                'order' => $order
            ]);
        } else {
            $this->error('查询失败');
        }

    }
    //查看全部订单的已接单
    public function index3(Request $request)
    {
        $order = OrderModel::order('id', 'DESC')
            ->where('members_id',$request->userId)
            ->where('switch',1)
            ->where('pay_type','<>',0)
            ->field('id,pet_image,price_name,status,pet_type,price')
            ->select();
        if ($order) {
            $this->success('查询成功', [
                'order' => $order
            ]);
        } else {
            $this->error('查询失败');
        }

    }
    //查看全部订单的服务中
    public function index4(Request $request)
    {
        $order = OrderModel::order('id', 'DESC')
            ->where('members_id',$request->userId)
            ->where('status',1)
            ->where('pay_type','<>',0)
            ->field('id,pet_image,price_name,status,pet_type,price')
            ->select();
        if ($order) {
            $this->success('查询成功', [
                'order' => $order
            ]);
        } else {
            $this->error('查询失败');
        }

    }
    //查看全部订单的已完成
    public function index5(Request $request)
    {
        $order = OrderModel::order('id', 'DESC')
            ->where('members_id',$request->userId)
            ->where('status',2)
            ->where('pay_type','<>',0)
            ->field('id,pet_image,price_name,status,pet_type,price')
            ->select();
        if ($order) {
            $this->success('查询成功', [
                'order' => $order
            ]);
        } else {
            $this->error('查询失败');
        }
    }


    //订单中心
    public function list(Request $request)
    {
        $id = input('id/d');

        $order = OrderModel::order('id','DESC')
//            ->where('master_id',$request->userId)
                ->where('master_id',$id)
            ->field('id,price_name,serve_time,end_time,num,remark,create_time,price,status')
            ->select();
        if ($order) {
            $this->success('查询成功', [
                'order' => $order
            ]);
        } else {
            $this->error('查询失败');
        }
    }
    //待服务
    public function list1(Request $request)
    {
        $id = input('id/d');
        $order = OrderModel::order('id','DESC')
            ->where('master_id',$id)
//            ->where('master_id',$request->userId)
                ->where('status',0)
            ->where('pay_type','<>',0)
            ->field('id,price_name,serve_time,end_time,num,remark,create_time,price,status')
            ->select();
        if ($order) {
            $this->success('查询成功', [
                'order' => $order
            ]);
        } else {
            $this->error('查询失败');
        }
    }
    //服务中
    public function list2(Request $request)
    {
        $id = input('id/d');
        $order = OrderModel::order('id','DESC')
            ->where('master_id',$id)
//            ->where('members_id',$request->userId)
            ->where('status',1)
            ->where('pay_type','<>',0)
            ->field('id,price_name,serve_time,end_time,num,remark,create_time,price,status')
            ->select();
        if ($order) {
            $this->success('查询成功', [
                'order' => $order
            ]);
        } else {
            $this->error('查询失败');
        }
    }

    //已完成
    public function list3(Request $request)
    {
        $id = input('id/d');
        $order = OrderModel::order('id','DESC')
            ->where('master_id',$id)
//            ->where('members_id',$request->userId)
            ->where('status',2)
            ->where('pay_type','<>',0)
            ->field('id,price_name,serve_time,end_time,num,remark,create_time,price,status')
            ->select();
        if ($order) {
            $this->success('查询成功', [
                'order' => $order
            ]);
        } else {
            $this->error('查询失败');
        }
    }

    //添加订单
    public function add(Request $request)
    {
        $data = input('post.');
        $validate = new OrderValidate();
        $res = $validate->scene('add')->check($data);
        if (!$res){
            $this->error($validate->getError());
        }
        $members = MembersModel::find($request->userId);
        $address = AddressModel::find($data['address_id']);
        $pet = PetModel::find($data['pet_id']);
        $price =PriceModel::find($data['price_id']);
//        halt(round($price['price'] * $data['offers'] * $data['num']));
        $pri = round($price['price'] * $data['offers'] * $data['num']);
//        halt($pri);
//        halt($request->userId);
//        halt($price['price']);
        $data1 = [
            'members_id' => $request->userId,
            'members_name' => $members['name'],
            'mobile' => $address['mobile'],
            'address_id' => $data['address_id'],
            'address' => $address['address'],
            'pet_id' => $data['pet_id'],
            'pet_name' => $pet['name'],
            'pet_image' => $pet['image'],
            'pet_type' => $pet['type'],
            'price_id' => $data['price_id'],
            'price_name' => $price['name'],
            'price_image' => $price['image'],
            'p_price' => $price['price'],
            'switch' => 0,
            'num' => $data['num'],
            'offers' => $data['offers'],
            'remark' => $data['remark'],
            'price' => $pri,
            'create_time' => date('Y-m-d H:i:s'),
            'status' => 0,
            'pay_type' => $data['pay_type']
        ];
        if (OrderModel::create($data1)){
            $this->success('添加成功');
        }
        $this->error('添加失败');
    }

    //师傅接单
    public function add2(Request $request)
    {
        $data = input('post.');
        if (empty($data)){
            $this->error('请传数据');
        }
        $order = OrderModel::find($data['id']);
        if (!$order){
            $this->error('该订单不存在');
        }
        $master = MasterModel::find($data['master_id']);

//        $order-> serve_time = date('Y-m-d H:i:s');
        $order-> serve_time = $data['serve_time'];
//        $order-> end_time = date('Y-m-d H:i:s',strtotime("+2 hours"));
        $order-> end_time = $data['end_time'];
        $order->switch = 1;
        $order-> master_id = $data['master_id'];
        $order-> master_name = $master['name'];

        if ($order->save()){
            $this->success('接单成功');
        }
        $this->error('接单失败');
    }

    //已支付
    public function pay(Request $request)
    {
        $data = input('post.');
        if (empty($data)){
            $this->error('未传输数据');
        }
        $order = OrderModel::find($data['id']);
        $order->pay_type = $data['pay_type'];
        $order->pay_time = date('Y-m-d H:i:s');
        if ($order->save()){
            $this->success('支付成功');
        }
        $this->error('支付失败');
    }

    //服务中
    public function edit1(Request $request)
    {
        $id = input('id/d');
        if (empty($id)){
            $this->error('未传输订单ID');
        }
        $order = OrderModel::find($id);
        $order->status = 1;
        if ($order->save()){
            $this->success('服务中');
        }
        $this->error('未开始服务');
    }

    //服务完成
    public function edit2(Request $request)
    {
        $id = input('id/d');
        if (empty($id)){
            $this->error('未传输订单ID');
        }
        $order = OrderModel::find($id);
        $order->status = 2;
        if ($order->save()){
            $this->success('已完成');
        }
        $this->error('服务结束不成功');
    }
    //订单详情
    public function detail(Request $request)
    {
        $id = input('id/d');
        if (empty($id)){
            $this->error('请输入订单ID');
        }
        $order = OrderModel::field('id,members_name,mobile,address,pet_name,pet_type,pet_image,serve_time,price_name,price_image,end_time,switch,master_name,num,remark,price,status')->find($id);
        if ($order) {
            $this->success('查询成功', [
                'order' => $order
            ]);
        } else {
            $this->error('查询失败');
        }
    }
}